package cn.psvmc.cxzapi.service.ticket;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.order.TOrderMapper;
import cn.psvmc.cxzapi.mapper.ticket.TTicketMapper;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.customer.TCustomer;
import cn.psvmc.cxzapi.model.ticket.TTicket;
import cn.psvmc.cxzapi.service.system.ServiceUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 工单表 服务实现类
 * @Author auto
 * @Date 2024-08-12
 */
@Service
public class TTicketService extends AbstractService<TTicketMapper, TTicket> {

    @Resource
    private TTicketMapper tTicketMapper;
    @Resource
    private TOrderMapper orderMapper;
    @Resource
    private ServiceUtil serviceUtil;

    /**
    * @Date auto 2024-08-12 新增工单表
    * @Param tTicket 工单表
    * @Return int
    */
    public int add(TTicket tTicket) {
        TCustomer customer = orderMapper.getPhoneByOrderCode(tTicket.getOrderCode());//根据订单编号获取客户手机号
        if (customer == null) throw new ServiceException("未查询到对应的客户信息！");
        String phone = customer.getPhone();
        tTicket.setPhone(phone);
        tTicket.setCustomerid(customer.getCustomerid());
        return tTicketMapper.insert(tTicket);
    }

    /**
    * @Date auto 2024-08-12 更新工单表
    * @Param tTicket 工单表模型
    * @Return int
    */
    public int edit(TTicket tTicket){
        // 通过id查询数据处理 详情
        TTicket originTTicket = this.detail(tTicket.getTicketId());
        if(originTTicket == null){
            throw new ServiceException("未查询到对应的工单表信息！");
        }

        // 比较数据版本号
        if(!originTTicket.getRversion().equals(tTicket.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }

        // 字段为null 不更新
        return tTicketMapper.updateById(tTicket);
    }

    /**
    * @Date auto 2024-08-12 查询工单表详情
    * @Param id 唯一id
    * @Return cn.psvmc.cxzapi.model.ticket.TTicket
    */
    public TTicket detail(Integer id){
        return tTicketMapper.selectById(id);
    }

    /**
    * @Date auto 2024-08-12 查询工单表列表
    * @Param iPage 分页
    * @Return com.baomidou.mybatisplus.core.metadata.IPage<cn.psvmc.cxzapi.model.ticket.TTicket>
    */
    public IPage<Map> list(Page<TTicket> iPage, SearchEntity entity){
        serviceUtil.setStoreId(entity);
        if (!ToolUtils.isTrueInteger(entity.getStoreid())) throw new ServiceException("商家id不能为空！");
        return tTicketMapper.listAll(iPage, entity);
    }

    /**
     * @Description 完结
     * @Date 2024/8/13 14:32
     * @Author YWX
     * @Param [id]
     * @Return void
     **/
    public void end(Integer id) {
        TTicket ticket = new TTicket();
        ticket.setTicketId(id);
        ticket.setStatus(1);
        ticket.setEndTime(LocalDateTime.now());
        tTicketMapper.updateById(ticket);
    }
}
